package com.xrui.hbase.codec.impl;

import com.google.common.base.Preconditions;
import com.xrui.hbase.codec.CellEncoder;
import com.xrui.hbase.codec.CellEncoderFactory;
import com.xrui.hbase.schema.spec.CellSpec;


import java.io.IOException;

/**
 * Factory for cell encoders.
 */
public final class DefaultCellEncoderFactory implements CellEncoderFactory {
    /**
     * Singleton instance.
     */
    private static final DefaultCellEncoderFactory SINGLETON =
        new DefaultCellEncoderFactory();

    /**
     * Singleton constructor.
     */
    private DefaultCellEncoderFactory() {
    }

    /**
     * @return the default factory for cell encoders.
     */
    public static DefaultCellEncoderFactory get() {
        return SINGLETON;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CellEncoder create(final CellSpec cellSpec) throws IOException {
        Preconditions.checkNotNull(cellSpec);
        switch (cellSpec.getCellSchema().getType()) {
            case AVRO:
                return new AvroCellEncoder(cellSpec);
            case PROTOBUF:
                return new ProtobufCellEncoder(cellSpec);
            case RAW_BYTES:
                return new RawBytesCellEncoder(cellSpec);
            case CUSTOM:
                return new CustomCellEncoder(cellSpec);
            default:
                throw new RuntimeException("Unhandled cell encoding: " + cellSpec.getCellSchema().getType());
        }
    }
}
